<%@page import="java.util.List"%>
<%@page import="dao.NewsDAO"%>
<%@page import="bean.News"%>
<%@ page import="util.MultipartFormUtil" %>
<%@ page import="java.io.ByteArrayInputStream" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻列表</title>
<link rel="stylesheet" type="text/css" href="../../css/Iframe.css" />
<link rel="stylesheet" type="text/css" href="../../css/bootstrap.min.css"
	media="screen" />
<link rel="stylesheet" type="text/css" href="../../css/layui.css" />
</head>

<body>
	<%
		String currentPage = request.getParameter("currentPage");
	if (currentPage == null) {
		currentPage = "1";
	}
	NewsDAO dao = new NewsDAO();
	//总新闻数
	int allRecords = dao.getAllRecords();
	//总页数
	int allPage = dao.getTotalPage(allRecords);
	//当前页面新闻内容
	int currentPageInt = Integer.parseInt(currentPage);
	if(currentPageInt < 1){
		currentPageInt = 1;
		out.print("<script>alert('已经是第一页了！')</script>");
	}else if(currentPageInt > allPage){
		currentPageInt = allPage;
		out.print("<script>alert('已经是最后一页了！')</script>");
	}
	List<News> list = dao.getCurrentPageNews(currentPageInt);
	session.setAttribute("newsList", list);
	%>
	<span class="cp_title">新闻列表</span>
	<div class="table_con">
		<div class="table_con">
			<table>
				<tr class="tb_title">
					<td width="5%">ID</td>
					<td width="10%">标题</td>
					<td width="35%">内容</td>
					<td width="10%">发布者</td>
					<td width="30%">时间</td>
					<td width="10%">图片</td>
				</tr>
				<%
					for (int i = 0; i < list.size(); i++) {
				%>
				<tr>
					<td width="5%"><%=list.get(i).getId()%></td>
					<td width="10%"><%=list.get(i).getTitle()%></td>
					<td width="35%"><%=list.get(i).getText()%></td>

<%--					<td width="63%"><a--%>
<%--						href="/News/IncreaseViews?index=<%=i%>&currentPage=<%=currentPageInt%>" class="del_btn"><%=list.get(i).getTitle()%></a></td>--%>
					<td width="10%"><%=list.get(i).getPublisher()%></td>
					<td width="30%"><%=list.get(i).getTime()%></td>

					<td width="10%">
						<%
							if(list.get(i).isHasImg()){
						%>
						<img width="100%" height="100%" src="data:image/png;base64,<%= java.util.Base64.getEncoder().encodeToString(MultipartFormUtil.toByteArray((ByteArrayInputStream)list.get(i).getImg())) %>" alt="图片">
						<%
							}
						%>
						<%
							if(!list.get(i).isHasImg()){
						%>
						未上传图片
						<%
							}
						%>
					</td>


				</tr>
				<%
					}
				%>
			</table>
			<div style="text-align: center">
				总计<%=allRecords %>条记录，共<%=allPage %>页，当前第<%=currentPageInt %>页&nbsp;
			</div><br>
			<div style="text-align: center">
				<ul class="pagination">
				<li><a
						href="newsList.jsp?currentPage=1">首页</a></li>
					<li><a
						href="newsList.jsp?currentPage=<%=currentPageInt - 1%>">上一页</a></li>
					<li><a
						href="newsList.jsp?currentPage=<%=currentPageInt + 1%>">下一页</a></li>
						<li><a
						href="newsList.jsp?currentPage=<%=allPage%>">尾页</a></li>
				</ul>
			</div><br>
		</div>
	</div>
	<script src="js/layui.js" charset="utf-8"></script>
</body>

</html>